package com.ciwong.epaper.modules.epaper.adapter;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.evaluate.ui.UserDkSubmitActivity;

import java.util.List;

public class SubmitPhotoGridAdapter extends BaseAdapter {

    private Context mContext;

    private List<String> list;

    private List<String> subList;

    private ImageView user_dk_child_submit;

    private ImageView delete_image_view;

    private LinearLayout user_submit_dk_count;

    private TextView user_submit_dk_count_text;


    private GridView gridView;

    int maxImgCount;

    private String subVideoPath;

    public SubmitPhotoGridAdapter(Context mContext, List<String> list,List<String> subList,String subVideoPath) {
        this.mContext = mContext;
        this.list = list;
        this.subList = subList;
        this.subVideoPath = subVideoPath;
    }


    @Override
    public int getCount() {
        if ( "".equals(subVideoPath) && list.size()>2){
            return 3;
        }else if ( !"".equals(subVideoPath) && list.size()>2){
            return 2;
        }else {
            return list.size();
        }
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

            view = View.inflate(mContext, R.layout.activity_dk_submit_child, null);
            user_dk_child_submit = (ImageView) view.findViewById(R.id.user_dk_child_submit);
            delete_image_view = (ImageView) view.findViewById(R.id.delete_image_view);
            user_submit_dk_count = (LinearLayout) view.findViewById(R.id.user_submit_dk_count);
            user_submit_dk_count_text = (TextView) view.findViewById(R.id.user_submit_dk_count_text);

        Glide.with(mContext).load(Uri.parse(list.get(i))).into(user_dk_child_submit);

            int count = list.size();
        if ("".equals(subVideoPath) && list.size()>3 && i == 2) {
            user_submit_dk_count_text.setText(String.valueOf(count));
            user_submit_dk_count.setVisibility(View.VISIBLE);
        }else if(!"".equals(subVideoPath) && list.size()>2 && i == 1){
            user_submit_dk_count_text.setText(String.valueOf(count));
            user_submit_dk_count.setVisibility(View.VISIBLE);
        }else {
            user_submit_dk_count.setVisibility(View.GONE);
        }
        delete_image_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(i);
                subList.remove(i);
                SubmitPhotoGridAdapter.this.notifyDataSetInvalidated();
                if (mContext instanceof UserDkSubmitActivity){
                    ((UserDkSubmitActivity) mContext).setImageView(subList);
                }
            }
        });
        return view;
    }







  /*  public void upDateList(List<String> newList){
        list.clear();
        list.addAll(newList);
    }

    public void listAdd(String u){
        list.add(u);
    }*/



}
